import { cascadelist, getOsversionList, getDeviceBrandList, getSceneNameList, getAdformatList, getAdvSceneList } from "@/api/common/index-manage.js";

export default {
  data () {
    return {
      // 项目数据
      projectList: [],
      // 产品数据
      productList: [],
      // 包名
      apppkgList: [],
      // 版本
      versionList: [],
      // 厂商
      deviceBrandList: [],
      // 安卓版本
      osversionList: [],
      // 场景
      sceneNameList: [],
      // 默认排序
      defaultSort: {prop: 'acdate', order: 'descending'},
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      tableHeight: 500
    }
  },
  mounted() {
    let elTable = document.querySelector('.el-table');
    if (elTable) {
      this.tableHeight = document.body.clientHeight - elTable.offsetTop - 150;
    }
    // window.onresize = () => {
    //   setTimeout(() => {
    //     this.tableHeight = document.body.clientHeight - elTable.offsetTop - 150;
    //   }, 300);
    // }
  },
  methods: {
    _getProjectList () {
      // cascadelist().then(response => {
      //   this.projectList = response.rows
      // })
    },
    _projectChange (newValue) {
      let vm = this;
      if (vm.open) {
        vm.$set(vm.form, 'productname', []);
        vm.$set(vm.form, 'apppkg', []);
        vm.$set(vm.form, 'version', []);
      } else {
        vm.$set(vm.queryParams, 'productname', []);
        vm.$set(vm.queryParams, 'apppkg', []);
        vm.$set(vm.queryParams, 'version', []);
        // vm.handleQuery();
      }
      cascadelist({projectname: Array.isArray(newValue) ? newValue.join(',') : newValue}).then(response => {
        vm.productList = response.rows;
      });
    },
    _productChange (newValue) {
      let vm = this;
      if (vm.open) {
        vm.$set(vm.form, 'apppkg', []);
        vm.$set(vm.form, 'version', []);
      } else {
        vm.$set(vm.queryParams, 'apppkg', []);
        vm.$set(vm.queryParams, 'version', []);
        // vm.handleQuery();
      }
      if (vm.needAppCode) {
        vm.$set(vm.queryParams, 'appcode', vm._getCurrItem('productList', 'productname', newValue));
      }
      cascadelist({productname: Array.isArray(newValue) ? newValue.join(',') : newValue}).then(respones => {
        vm.apppkgList = respones.rows;
      });
    },
    _apppkgChange (newValue) {
      let vm = this;
      if (vm.open) {
        vm.$set(vm.form, 'version', []);
      } else {
        vm.$set(vm.queryParams, 'version', []);
        // vm.handleQuery();
      }
      // vm.queryParams.version = [];
      cascadelist({apppkg: Array.isArray(newValue) ? newValue.join(',') : newValue}).then(respones => {
        vm.versionList = respones.rows;
      });
    },
    _getCurrItem (listKey, key, value) {
      let vm = this;
      return vm[listKey].filter(item => {
        return value.includes(item[key]);
      }).map(item => {
        return item.appcode;
      });
    },
    // 获取厂商列表
    _getDeviceBrandList () {
      let vm = this;
      getDeviceBrandList().then(respones => {
        vm.deviceBrandList = respones.map(item => {
          return {
            label: item,
            value: item
          }
        });
      })
    },
    // 获取安卓版本列表
    _getOsversionList () {
      let vm = this;
      getOsversionList().then(respones => {
        vm.osversionList = respones.map(item => {
          return {
            label: item,
            value: item
          }
        });
      })
    },
    // 获取场景列表
    _getSceneNameList () {
      let vm = this;
      getSceneNameList().then(respones => {
        vm.sceneNameList = respones.map(item => {
          return {
            label: item,
            value: item
          }
        });
      })
    },
    // 获取广告类型
    _getAdformatList () {
      let vm = this;
      getAdformatList().then(response => {
        vm.adformatList = response.map(item => {
          return {
            label: item,
            value: item
          }
        });
      })
    },
    // 获取广告场景
    _getAdvSceneList () {
      let vm = this;
      getAdvSceneList().then(response => {
        vm.advSceneList = response.map(item => {
          return {
            label: item,
            value: item
          }
        });
      });
    },
    // 表头排序
    _changeSort (column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList(); //调用列表
    },
    // 整合参数
    _integrationParams () {
      let vm = this
      if (!vm.queryParams.orderByColumn) vm.queryParams.orderByColumn = vm.defaultSort.prop;
      if (!vm.queryParams.isAsc) vm.queryParams.isAsc = vm.defaultSort.order;
      if (vm.queryParams.registerTime) {
        vm.queryParams.registerdate = vm.queryParams.registerTime.replace(/-/g, '');
      } else {
        vm.queryParams.registerdate = '';
      }
      if (vm.queryParams.dateRange && vm.queryParams.dateRange.length) {
        vm.queryParams.beginTime = vm.queryParams.dateRange[0].replace(/-/g, '');
        vm.queryParams.endTime = vm.queryParams.dateRange[1].replace(/-/g, '');
        vm.queryParams.beginDate = vm.queryParams.beginTime;
        vm.queryParams.endDate = vm.queryParams.endTime;
        vm.queryParams.begindate = vm.queryParams.beginTime;
        vm.queryParams.enddate = vm.queryParams.endTime;
      } else {
        vm.queryParams.beginTime = '';
        vm.queryParams.endTime = '';
        vm.queryParams.beginDate = '';
        vm.queryParams.endDate = '';
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.$refs["queryForm"].validate(valid => {
        if (valid) {
          this.queryParams.disableSearch = true;
          this.queryParams.pageNum = 1;
          this.getList();
          setTimeout(() => {
            this.queryParams.disableSearch = false;
          }, 3000);
        }
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
}
